﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using ZO.SmartCore.Collections.Generic;

#endregion

namespace ZO.SmartCore.Xml
{
    /// <summary>
    /// Represents a collection of attribute objects 
    /// </summary>
    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
    public class AttributeList : IEnumerable<KeyValuePair<string, Attribute>>
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeList"/> class.
        /// </summary>
        /// <param name="element">The element.</param>
        internal AttributeList(Element element)
        {
            this.parentElement = element;
        }

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private Element parentElement;
        private OrderedDictionary<string, Attribute> _InternalAttributes = new OrderedDictionary<string, Attribute>();

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        /// <summary>
        /// Gets an Dictionary containing the list of attributes for this node. 
        /// </summary>
        /// <remarks>Do Not</remarks>
        private OrderedDictionary<string, Attribute> InternalAttributes
        {
            get
            {
                return this._InternalAttributes;
            } // get
        }

        /// <summary>
        /// Gets or sets the value with the specified key.
        /// </summary>
        /// <value></value>
        public string this[string name]
        {
            get
            {
                if (this.InternalAttributes.Contains(name))
                {
                    return this.InternalAttributes[name].Value;
                }
                return String.Empty;
            }
            set
            {
                if (this.InternalAttributes.Contains(name))
                {
                    this.InternalAttributes[name].Value = value;
                }
                else
                {
                    this.InternalAttributes.Add(name, new Attribute(name, value));
                }
            }
        }


        /// <summary>
        /// Gets the number of elements contained in the collection.
        /// </summary>
        /// <value></value>
        /// <returns>The number of elements contained in the collection.</returns>
        public int Count
        {
            get { return this.InternalAttributes.Count; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds the attribute.
        /// </summary>
        /// <param name="name">The attribute name.</param>
        /// <param name="value">The attribute value.</param>
        public void Add(string name, string value)
        {
            if (String.IsNullOrEmpty(name))
            {
                return;
            }

            Attribute attr = new Attribute(name, value);

            attr.parentElement = this.parentElement;

            this.InternalAttributes.Add(name, attr);


        }

        /// <summary>
        /// Adds the attribute.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The attribute value.</param>
        /// <param name="prefix">The prefix.</param>
        public void Add(string name, string value, string prefix)
        {
            if (String.IsNullOrEmpty(name))
            {
                return;
            }

            Attribute attr = new Attribute(name, value, prefix);

            attr.parentElement = this.parentElement;

            this.InternalAttributes.Add(name, attr);

        }

        /// <summary>
        /// Adds the attribute.
        /// </summary>
        /// <param name="attr">The attr.</param>
        public void Add(Attribute attr)
        {
            if (attr == null)
            {
                return;
            }

            attr.parentElement = this.parentElement;

            this.InternalAttributes.Add(attr.Name, attr);
        }

        /// <summary>
        /// Removes the attribute.
        /// </summary>
        /// <param name="name">The attribute name.</param>
        public void Remove(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                return;
            }

            if (this.Contains(name))
            {
                this.InternalAttributes.Remove(name);
            }
        }


        /// <summary>
        /// Determines whether [contains] [the specified prefix].
        /// </summary>
        /// <param name="name">The attribute name.</param>
        /// <returns>
        /// 	<c>true</c> if [contains] [the specified prefix]; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                return false;
            }
            if (InternalAttributes.Contains(name))
            {
                return true; 
            }

            return false;
        }

        /// <summary>
        /// Removes all items
        /// </summary>
        /// <exception cref="System.NotSupportedException">The collection is read-only. </exception>
        public void Clear()
        {
            this.InternalAttributes.Clear();
        }


        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            bool firstTime = true;
            foreach (Attribute attr in this.InternalAttributes.Values)
            {
                if (!firstTime)
                {
                    sb.Append(" ");
                }
                sb.Append(attr.ToString());
                firstTime = false;
            }
            return sb.ToString();
        }
        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<KeyValuePair<string, Attribute>> GetEnumerator()
        {
            return this.InternalAttributes.GetEnumerator();
        }


        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.InternalAttributes.GetEnumerator();
        }


        /// <summary>
        /// Gets an collection containing the names of the attributes in <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>An collection containing the values of the object that implements <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.</returns>
        public virtual ICollection<string> Names
        {
            get
            {
                return this.InternalAttributes.Keys;
            }
        }

        /// <summary>
        /// Gets an collection containing the values in the <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>An collection containing the values in the object that implements <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.</returns>
        public virtual ICollection<Attribute> Values
        {
            get
            {

                return this.InternalAttributes.Values;
            } // get
        }

        #endregion

    }
}
